package com.mdjukebox.http.controller.actions;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import com.util.struts.actions.DefaultAction;
import com.util.exceptions.InternalErrorException;
import com.util.sql.PlainActionProcessor;

import com.mdjukebox.model.adminfacade.actions.CancionesReproducidasAction;
import com.mdjukebox.model.adminfacade.actions.ListadoPeticionesAction;

/**
 * 
 * 
 *
 */
public class PantallaReproduccion extends DefaultAction{
	
	public ActionForward doExecute(ActionMapping mapping,
	        ActionForm form, HttpServletRequest request,
	        HttpServletResponse response)
	        throws IOException, ServletException, InternalErrorException {
	        
        /* Do . */
		ActionMessages errors = new ActionMessages();
		try {
			//ultimas canciones reproducidas
			CancionesReproducidasAction action = new CancionesReproducidasAction();
            ArrayList reproducidas = (ArrayList) PlainActionProcessor.process(action);

            //cola de peticiones
			ListadoPeticionesAction action2 = new ListadoPeticionesAction();
            ArrayList cola = (ArrayList) PlainActionProcessor.process(action2);
			//Collection cola = facade.findByTypeEventLog(EventLogVO.RECEPCION);
			
            request.setAttribute("reproducidas", reproducidas);
            request.setAttribute("cola", cola);
            
//            throw new Exception("forzado");
            
        } catch (Exception e) {
        	if (log.isErrorEnabled()) log.error("Pantalla Reproduccion: ",e);
            errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
                    "Listado.Error", e.getMessage()));
            //si algo va mal mostrar pantalla vacia e ir a ver al log
            request.setAttribute("reproducidas", new ArrayList());
            request.setAttribute("cola", new ArrayList());
            
        } 
        
        /* Return ActionForward. */
        if (!errors.isEmpty()) {
        	saveErrors(request, errors);
        }
            
        return  mapping.findForward("pantalla");
        
    }

}
